package com.vf.admin.platform.dao;

import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.platform.entity.Role;
import com.vf.admin.utils.StringEx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 平台基础角色
 */
@Repository
public class RoleDao extends BaseDao<Role> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 保存或者更新
     */
    public void saveOrUpdate(Role role){
        if(role.getId()!=null && role.getId() > 0){
            this.update(role);
        }else{
            role.setId(this.add(role));
        }
    }
    //详情
    public Role get(Integer id){
        if(StringEx.isNullOrLing(id)){
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_role  where 1=1");
        sbStr.append(" and id = ? ");
        sbStr.append(" ORDER BY sorts DESC ");
        paramList.add(id);

        String sql = sbStr.toString();
        return this.queryObj(sql,paramList, Role.class);
    }

    //列表查询
    public List<Role> findList(Map<String,Object> paraMap, SplitPageBean splitPage){
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from bt_role  where 1=1");
        if(StringEx.isNotNull(paraMap.get("name"))){
            sbStr.append(" and name like ? ");
            paramList.add("%"+paraMap.get("name")+"%");
        }
        if(StringEx.isNotNull(paraMap.get("states"))){
            sbStr.append(" and states = ? ");
            paramList.add(paraMap.get("states"));
        }
        sbStr.append(" ORDER BY sorts DESC ");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList,Role.class,splitPage);
    }


    /**
     * 删除
     */
    public void del(int id){
        if (id <= 0) {
            return;
        }
        String sql = "delete  from bt_role  where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }
}
